Plots corresponding to Moving-Average(weekly), Moving-Average(Monthly), Moving-Average(Quarterly)
Calculating Daily returns based on percent
Percent change can provide insights into the volatility and distribution of
the stock returns over time
pip install pandas_ta
List of Financial Indicators employed:-
['RSI: Relative Strength Index', 'ADL: Accumulation/Distribution Line', 'ATR: Average True Range', 'MOM: Momentum', 'MFI: Money Flow Index', 'ROC: Rate of Change', 'OBV: On-Balance Volume', 'CCI: Commodity Channel Index']
Text(0.5, 0, 'Correlation')
(2601, 10)
(2601, 5)
The parameter Adjusted Close i.e. Adj Close will be the target veriable for model building and prediction as it is the closing price after adjustments for all applicable splits and dividend distributions. The data for futher processing is of shape (2601, 5)
The Train-Validation-Test Split --------------------********-------------------- Length of train data is 2081, Length of Validation data is 312, Length of Test data is 208 --------------------^^^^^^^^--------------------
scaler_ys = MinMaxScaler(feature_range=(0,1)) scaler_y = scaler_ys.fit(np.array(train_.iloc[:training_len,0]).reshape(-1,1))
(2081,)
((2081, 5), (2601, 5), (2601, 5))
Training data is dated from 2013-01-21 to 2021-07-01 i.e. 2081 days --------------------------------------------------------------------------- Validation will begin from 2021-07-02 to 2022-10-03 i.e 312 days --------------------------------------------------------------------------- lastly, Test data is from 2022-10-04 to 2023-08-04 i.e 208 days --------------------------------^^^^^^^^^^^--------------------------------
For Multivariate LSTM modelling The splitting is now shaped as follows after running it throught the generate_data function The data is currently structured as a 3D tensor with a dimension of 10 timestamps and 5 features ---------------------------------------------------- train shape: (2070, 10, 5) (2070,) (2070,) validation: (301, 10, 5) (301,) (301,) test shape: (197, 10, 5) (197,) (197,)
For Univariate LSTM modelling The splitting is now shaped as follows after running it throught the generate_data_univerate function 10 columns generated, having adj close price prior to 10 days will be used as X_train while next-day will be y_train The corresponding are the X,Y and Date array shapes ---------------------------------------------------- train shape: (2070, 10) (2070,) (2070,) validation: (301, 10) (301,) (301,) test shape: (197, 10) (197,) (197,)
Converting data into a 3d array for Univariate LSTM
train data shape for univerate: X: (2070, 10, 1) | y: (2070,)
validation shape for univerate: X: (301, 10, 1) | y: (301,)
test data shape for univerate: X: (197, 10, 1) | y: (197,)
Model: "univariate_LSTM"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_13 (LSTM) (None, 10, 128) 66560
lstm_14 (LSTM) (None, 64) 49408
dropout_11 (Dropout) (None, 64) 0
dense_13 (Dense) (None, 25) 1625
dense_14 (Dense) (None, 1) 26
=================================================================
Total params: 117,619
Trainable params: 117,619
Non-trainable params: 0
_________________________________________________________________
Model: "Multivariate-LSTM"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_62 (LSTM) (None, 10, 128) 68608
dropout_50 (Dropout) (None, 10, 128) 0
lstm_63 (LSTM) (None, 64) 49408
dropout_51 (Dropout) (None, 64) 0
dense_75 (Dense) (None, 25) 1625
batch_normalization_13 (Bat (None, 25) 100
chNormalization)
dense_76 (Dense) (None, 12) 312
dense_77 (Dense) (None, 1) 13
=================================================================
Total params: 120,066
Trainable params: 120,016
Non-trainable params: 50
_________________________________________________________________
deleted model deleted history
[datetime.date(2010, 1, 4), '---', datetime.date(2023, 8, 4), 2666]
(2666, 7, 5)
Epoch 1/15 27/27 [==============================] - 1s 23ms/step - loss: 0.0041 - val_loss: 0.0049 Epoch 2/15 27/27 [==============================] - 1s 22ms/step - loss: 0.0027 - val_loss: 0.0015 Epoch 3/15 27/27 [==============================] - 1s 24ms/step - loss: 0.0033 - val_loss: 0.0018 Epoch 4/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0020 - val_loss: 0.0013 Epoch 5/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0023 - val_loss: 0.0014 Epoch 6/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0015 - val_loss: 0.0012 Epoch 7/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0017 - val_loss: 0.0011 Epoch 8/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0011 - val_loss: 0.0013 Epoch 9/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0014 - val_loss: 0.0011 Epoch 10/15 27/27 [==============================] - 1s 21ms/step - loss: 9.1076e-04 - val_loss: 0.0011 Epoch 11/15 27/27 [==============================] - 1s 21ms/step - loss: 0.0010 - val_loss: 0.0010 Epoch 12/15 27/27 [==============================] - 1s 23ms/step - loss: 8.6512e-04 - val_loss: 0.0011 Epoch 13/15 27/27 [==============================] - 1s 23ms/step - loss: 9.4404e-04 - val_loss: 0.0011 Epoch 14/15 27/27 [==============================] - 1s 22ms/step - loss: 8.7484e-04 - val_loss: 0.0011 Epoch 15/15 27/27 [==============================] - 1s 22ms/step - loss: 9.2262e-04 - val_loss: 0.0010
Univariate LSTM Model
7/7 [==============================] - 0s 7ms/step
65/65 [==============================] - 1s 8ms/step
Considering 10 previous date data for model training the performance is as follows
After 40 epochs,
Train RMSE = 12.168
Test RMSE = 14.963
Traning the Multivariate model for:-
Gave the best fit on test data with RMSE as under
Multivariate LSTM Model
7/7 [==============================] - 0s 7ms/step
65/65 [==============================] - 0s 7ms/step
Considering 10 previous date data for model training the performance is as follows
After 230 epochs,
Train RMSE = 11.432
Test RMSE = 11.042
It is not unusual for the validation loss and training loss to differ in LSTM modeling.
The most important thing to look for is how well the model is performing on the test data.
If the test data fit and test RMSE are good, that indicates that the model is able to accurately predict new data.
Scaled_Train RMSE = 0.020 Scaled_Test RMSE = 0.023 Train RMSE = 10.783 Test RMSE = 12.069
This algorithm calculates the stock's performance from the succeeding "duration_".
Upon executing the algorithm,
the user will be prompted to enter the desired "Date" and the number of days for predictions.
Subsequently, a graph representing the generated predictions will be displayed.
Additionally, there is an option to save the predictions as a CSV file for further reference.
Forecasting with Multivariate LSTM Model
The Model predicts based on the data of 10
kindly,
wait for predictions
future value of stock on 2023-08-05 is predicted to be 629.108